﻿namespace Pibby.Core
{
	using System;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.Linq;
	
	public class EpisodeCollection : Collection<Episode>
	{
		public EpisodeCollection()
		{
		}

		public EpisodeCollection(IList<Episode> episodes)
			: base(episodes)
		{
		}

		#region Indexing

		public Episode this[string id]
		{
			get
			{
				return this.Where(episode => episode.Id == id)
							.FirstOrDefault();
			}
		}

		public Episode FindByFileName(string fileName)
		{
			return this.Where(episode => StringComparer.OrdinalIgnoreCase.Equals(episode.FileName, fileName))
						.FirstOrDefault();
		}

		#endregion

		#region Adding 

		public void AddRange(IEnumerable<Episode> collection)
		{
			foreach (Episode episode in collection)
			{
				base.Add(episode);
			}
		}

		#endregion

		public void SortByName()
		{
			List<Episode> items = (List<Episode>)this.Items;

			// sort descending
			items.Sort(delegate(Episode e1, Episode e2) { return e2.Name.CompareTo(e1.Name); });
		}

		public void SortByDate()
		{
			List<Episode> items = (List<Episode>)this.Items;

			// sort descending
			items.Sort(delegate(Episode e1, Episode e2) { return e2.PublicationDate.CompareTo(e1.PublicationDate); });
		}
	}
}
